// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	{
//#if (TARGET_OS == OS_WINDOWS)
//#ifdef _DEBUG
//		_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//#endif
//		DisableThreadLibraryCalls((HMODULE)hModule);
//#endif
	}
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}


_stdmethod_export(HRESULT, GetLicenseCode)(const char* id, LicenseProc proc, void** lpContext, ULONG* size) {

	rc_assert(proc != NULL, E_FAIL);

	basic_tstring	strLicenseFile;

	TCHAR szPath[MAX_PATH + 1] = { 0x00 };

	GetExecutePath(INSNULL, szPath, MAX_PATH);

	strLicenseFile.clear();
	strLicenseFile.append(szPath);
	strLicenseFile.append(path_slash);
	strLicenseFile.append(_T("bin.dat"));

	_fd_t		hFile = INVALID_HANDLE_VALUE;
	hFile = _file_open(strLicenseFile.c_str(), O_RDWR, 0644);
	rc_assert(hFile != INVALID_HANDLE_VALUE, S_ERROR);

	size_t ulFileLen = _file_fsize(hFile) & UINT32_MAX;
	rc_assert(ulFileLen, S_ERROR);

	SafePtr<unsigned char*, default_safeptr> m_pdata;
	
	m_pdata = ALLOC_NEW unsigned char[ulFileLen + 1]();

	rc_assert(m_pdata != NULL, S_ERROR);

	size_t read = ulFileLen & UINT32_MAX;

	_file_read(hFile, m_pdata, read);
	_file_close(hFile);

	proc(m_pdata, read, lpContext);

	*size = ulFileLen;

	return S_OK;
}
